﻿#include "ComboBoxPropertyEditEntry.h"
#include <QComboBox>

#include "BooleanEditEntry.h"
#include "IntegerEditEntry.h"
#include "EnumEditEntry.h"
#include "SizeEditEntry.h"
#include "UiTextEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(ComboBox)

struct ComboBoxPropertyEditEntry::ComboBoxPropertyEditEntryPrivate {
    QPointer<QComboBox> object;

    QPointer<BooleanEditEntry> entryEditable;
    QPointer<UiTextEditEntry> entryCurrentText;
    QPointer<IntegerEditEntry> entryCurrentIndex;
    QPointer<IntegerEditEntry> entryMaxVisibleItems;
    QPointer<IntegerEditEntry> entryMaxCount;
    QPointer<EnumEditEntry> entryInsertPolicy;
    QPointer<EnumEditEntry> entrySizeAdjustPolicy;
    QPointer<IntegerEditEntry> entryMinimumContentsLength;
    QPointer<SizeEditEntry> entryIconSize;
    QPointer<UiTextEditEntry> entryPlaceHolderText;
    QPointer<BooleanEditEntry> entryDuplicatesEnabled;
    QPointer<BooleanEditEntry> entryFrame;
    QPointer<IntegerEditEntry> entryModelColumn;
};

ComboBoxPropertyEditEntry::~ComboBoxPropertyEditEntry() { }

ComboBoxPropertyEditEntry::ComboBoxPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new ComboBoxPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void ComboBoxPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QComboBox*>(object);
}

void ComboBoxPropertyEditEntry::onTick() {
    if(!p->object || !isVisible()) {
        return ;
    }

    p->entryEditable->setValue(p->object->isEditable());
    p->entryCurrentText->setValue(p->object->currentText());
    p->entryCurrentIndex->setValue(p->object->currentIndex());
    p->entryMaxVisibleItems->setValue(p->object->maxVisibleItems());
    p->entryMaxCount->setValue(p->object->maxCount());
    p->entryInsertPolicy->setValue(p->object->insertPolicy());
    p->entrySizeAdjustPolicy->setValue(p->object->sizeAdjustPolicy());
    p->entryMinimumContentsLength->setValue(p->object->minimumContentsLength());
    p->entryIconSize->setValue(p->object->iconSize());
    p->entryPlaceHolderText->setValue(p->object->placeholderText());
    p->entryDuplicatesEnabled->setValue(p->object->duplicatesEnabled());
    p->entryFrame->setValue(p->object->hasFrame());
    p->entryModelColumn->setValue(p->object->modelColumn());
}

void ComboBoxPropertyEditEntry::initUi() {
    p->entryEditable = new BooleanEditEntry;
    appendChildEntry(p->entryEditable);

    p->entryCurrentText = new UiTextEditEntry;
    appendChildEntry(p->entryCurrentText);

    p->entryCurrentIndex = new IntegerEditEntry;
    appendChildEntry(p->entryCurrentIndex);

    p->entryMaxVisibleItems = new IntegerEditEntry;
    appendChildEntry(p->entryMaxVisibleItems);

    p->entryMaxCount = new IntegerEditEntry;
    appendChildEntry(p->entryMaxCount);

    p->entryInsertPolicy = new EnumEditEntry;
    p->entryInsertPolicy->setMetaEnum(QMetaEnum::fromType<QComboBox::InsertPolicy>());
    appendChildEntry(p->entryInsertPolicy);

    p->entrySizeAdjustPolicy = new EnumEditEntry;
    p->entrySizeAdjustPolicy->setMetaEnum(QMetaEnum::fromType<QComboBox::SizeAdjustPolicy>());
    appendChildEntry(p->entrySizeAdjustPolicy);

    p->entryMinimumContentsLength = new IntegerEditEntry;
    appendChildEntry(p->entryMinimumContentsLength);

    p->entryIconSize = new SizeEditEntry;
    appendChildEntry(p->entryIconSize);

    p->entryPlaceHolderText = new UiTextEditEntry;
    appendChildEntry(p->entryPlaceHolderText);

    p->entryDuplicatesEnabled = new BooleanEditEntry;
    appendChildEntry(p->entryDuplicatesEnabled);

    p->entryFrame = new BooleanEditEntry;
    appendChildEntry(p->entryFrame);

    p->entryModelColumn = new IntegerEditEntry;
    appendChildEntry(p->entryModelColumn);
}

void ComboBoxPropertyEditEntry::retranslateUi() {
    setTitle("QComboBox");
    p->entryEditable->setTitle(tr("editable"));
    p->entryCurrentText->setTitle(tr("currentText"));
    p->entryCurrentIndex->setTitle(tr("currentIndex"));
    p->entryMaxVisibleItems->setTitle(tr("maxVisibleItems"));
    p->entryMaxCount->setTitle(tr("maxCount"));
    p->entryInsertPolicy->setTitle(tr("insertPolicy"));
    p->entrySizeAdjustPolicy->setTitle(tr("sizeAdjustPolicy"));
    p->entryMinimumContentsLength->setTitle(tr("minimumContentsLength"));
    p->entryIconSize->setTitle(tr("iconSize"));
    p->entryPlaceHolderText->setTitle(tr("placeHolderText"));
    p->entryDuplicatesEnabled->setTitle(tr("duplicatesEnabled"));
    p->entryFrame->setTitle(tr("frame"));
    p->entryModelColumn->setTitle(tr("modelColumn"));
}
